package com.woniuxy.woniuhis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageInfo;
import com.woniuxy.woniuhis.entity.Emp;
import com.woniuxy.woniuhis.entity.Office;
import com.woniuxy.woniuhis.entity.Patient;
import com.woniuxy.woniuhis.entity.RegisInfo;
import com.woniuxy.woniuhis.entity.RegisType;
import com.woniuxy.woniuhis.mapper.EmpMapper;
import com.woniuxy.woniuhis.service.OfficeService;
import com.woniuxy.woniuhis.service.OutpatientService;
import com.woniuxy.woniuhis.service.PatientService;
import com.woniuxy.woniuhis.service.RegisInfoService;
import com.woniuxy.woniuhis.service.RegisTypeService;
import com.woniuxy.woniuhis.util.ResponseJson;

@Controller
@RequestMapping("/regisInfo")
public class RegisInfoController {
	@Autowired
	RegisInfoService regisInfoService;
	@Autowired
	PatientService patientService;
	@Autowired
	OutpatientService outpatientService;
	@Autowired
	RegisTypeService regisTypeService;
	@Autowired
	OfficeService officeService;
	@Autowired
	EmpMapper empMapper;
	/**
	 * 挂号首页
	 * @return
	 */
	@RequestMapping("/regisInfoIndex")
	public ModelAndView regisInfo() {
		ModelAndView mv = new ModelAndView();
		//查询所有患者
		List<Patient> patients = patientService.findAllPatients();
		//查询所有挂号类别
		List<RegisType> regisTypes = regisTypeService.findAllRegisTypes();
		//查询所有员工
		List<Emp> emps = empMapper.selectEmps(new Emp());
		//查询所有科室
		PageInfo<Office> page = officeService.findAllOffices(new Office(),1,5);
		List<Office> offices = page.getList();
		
		
		mv.addObject("patients", patients);
		mv.addObject("regisTypes", regisTypes);
		mv.addObject("emps", emps);
		mv.addObject("offices", offices);
		mv.setViewName("views/outpatient/regisInfoAdd");
		return mv;
	}
	
	/*
	 * 添加挂号信息
	 */
	@RequestMapping("/regisInfoAdd")
	@ResponseBody
	public String regisInfoAdd(@RequestBody RegisInfo regisInfo) {
		try {
			regisInfoService.addRegisInfo(regisInfo);
			return "提交成功";
		} catch (Exception e) {
			e.printStackTrace();
			return "系统维护中";
		}
	} 
	/*
	 * 查询当日挂号记录
	 */
	@RequestMapping("/todayRegisInfo")
	@ResponseBody
	public ResponseJson<RegisInfo> todayRegisInfo() {
		ResponseJson<RegisInfo> regisInfoResponseJson = null;
		try {
			regisInfoResponseJson = regisInfoService.todayRegisInfo();
			return regisInfoResponseJson;
		} catch (Exception e) {
			regisInfoResponseJson.setCode(1);
			regisInfoResponseJson.setMsg(e.getMessage());
			return regisInfoResponseJson;
		}
	}
	
	/**
	 * 挂号统计
	 */
	@RequestMapping("regisStatistics")
	public ModelAndView regisStatistics() {
		ModelAndView mv=new ModelAndView();
		//当日挂号总人数,参数为0表示今日，参数为1表示昨日
		Integer todayRegisCount = regisInfoService.regisCount(0);
		//当日已完成就诊人数
		Integer todayFinishOutpatient = outpatientService.outpatientStatus(true,true);
		//正在就诊人数
		Integer outpatienting = outpatientService.outpatientStatus(true,false);
		//已挂号未缴费人数
		Integer regisNotPay = regisInfoService.notPayStatus();
		
		//不同科室昨日挂号情况
		Integer[] yesterdayRegisOfficeCount = regisInfoService.regisOfficeCount(1);
		String yesterdayRegisOffice = "";
		for(Integer i:yesterdayRegisOfficeCount) {
			yesterdayRegisOffice = yesterdayRegisOffice+","+i;
		}
		yesterdayRegisOffice = yesterdayRegisOffice.substring(1);
		System.out.println(yesterdayRegisOffice);
		//不同科室今日挂号情况
		Integer[] todayRegisOfficeCount = regisInfoService.regisOfficeCount(0);
		String todayRegisOffice = "";
		for(Integer i:todayRegisOfficeCount) {
			todayRegisOffice = todayRegisOffice+","+i;
		}
		todayRegisOffice = todayRegisOffice.substring(1);
		System.out.println(todayRegisOffice);
		
		mv.addObject("todayRegisCount", todayRegisCount);
		mv.addObject("todayFinishOutpatient", todayFinishOutpatient);
		mv.addObject("outpatienting", outpatienting);
		mv.addObject("regisNotPay", regisNotPay);
		mv.addObject("yesterdayRegisOffice", yesterdayRegisOffice);
		mv.addObject("todayRegisOffice", todayRegisOffice);
		mv.setViewName("views/outpatient/regisStatistics");
		
		return mv;
	}

}
